class MyEvent extends egret.DisplayObjectContainer {

    public constructor() {
        super();

        var boy:Boy = new Boy();
        boy.name = '小明';

        var girl:Girl = new Girl();
        girl.name = '小红';

        boy.addEventListener(DateEvent.DATE, girl.getDate, girl);
        boy.order();
        boy.removeEventListener(DateEvent.DATE, girl.getDate, girl);

        //检测侦听器
        console.log(boy.hasEventListener(DateEvent.DATE));

        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this)
    }

    onAddToStage() {
        var rect:egret.Sprite = new egret.Sprite();
        rect.graphics.beginFill(0x00ff00, 1);
        rect.graphics.drawRect(100, 200, 100, 80);
        rect.graphics.endFill();
        rect.width = 200;
        rect.height = 200;
        this.addChild(rect);

        rect.touchEnabled = true;
        rect.addEventListener( egret.TouchEvent.TOUCH_TAP, this.onTouch, this );
        this.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTouchTap, this);
        this.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTouchTaps, this, true);
    }

    onTouch(evt:egret.TouchEvent) {
        console.log('rect onTouch', evt);
    }

    onTouchTap(evt:egret.TouchEvent) {
        console.log('this onTouchTap', evt);
    }

    onTouchTaps(evt:egret.TouchEvent) {
        console.log('this onTouchTaps', evt);
    }
}

class Boy extends egret.Sprite {

    public constructor() {
        super();
    }

    public order() {
        var dateEvent:DateEvent = new DateEvent(DateEvent.DATE);
        dateEvent._year = 2018;
        dateEvent._month = 12;
        dateEvent._date = 12;
        dateEvent._where = "德克士";
        dateEvent._todo = "吃";
        this.dispatchEvent(dateEvent);
    }
}

class Girl extends egret.Sprite {

    public constructor() {
        super();
    }

    public getDate(evt:DateEvent) {
        console.log(evt);
        console.log("得到了" + evt.target.name + "的邀请！" );
        console.log("会在" + evt._year + "年" + evt._month + "月" + evt._date + "日，在"+ evt._where+ evt._todo);
    }
}

class DateEvent extends egret.Event {

    public static DATE:string = "约会";
    public _year:number = 0;
    public _month:number = 0;
    public _date:number = 0;
    public _where:string = "";
    public _todo:string = "";

    public constructor(type:string, bubbles:boolean=false, cancelable:boolean=false) {
        super(type,bubbles,cancelable);
    }
}